package com.weishe.weichar;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class WeathMapper extends Mapper<LongWritable, Text, Weather, Text> {

	public static Calendar c = Calendar.getInstance();
	public static SimpleDateFormat SDF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		String input = value.toString();
		String[] inputs = input.split("\t");
		try {
			Date date=SDF.parse(inputs[0]);
			
			c.setTime(date);
			int year = c.get(Calendar.YEAR);
			int month=c.get(Calendar.MONTH);
			
			String t=inputs[1];
			String t2=t.substring(0, t.length()-2);
			double tp = Double.parseDouble(t2);
			Weather w = new Weather(year, month, tp);
			context.write(w, value);
		} catch ( Exception e) {
 			e.printStackTrace();
		}

	
	}
}
